package us.icitap.timsportal.management;

import java.util.List;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.VerticalLayout;

import us.icitap.timsportal.ejb.PortalBeanLocal;
import us.icitap.timsportal.entity.OrgStructure;
import us.icitap.timsportal.resources.TimsPortalLabels;
import us.icitap.timsportal.util.SearchListener;
import us.icitap.timsportal.web.custom.PNativeButton;
import us.icitap.timsportal.web.custom.PTextField;
import us.icitap.timsportal.web.custom.PWindow;

@SuppressWarnings("serial")
public class OrgStructureWindow extends PWindow implements ClickListener {
	
	private SearchListener<?> searchListener;
	
	private PTextField searchField;
	private PNativeButton searchBtn;
	private Table table;
	private BeanItemContainer<OrgStructure> container;

	private PortalBeanLocal portalBean;
	
	private static final Object[] COL_ORDER = new Object[]{"id", "name", "status"};
	private static final String[] HEADERS = new String[]{"ID", "STRUKTURA", "STATUS"};
	
	public OrgStructureWindow(){
		setModal(true);
		setWidth("60%");
		
		searchField = new PTextField();
		searchField.setWidth("200px");
		searchField.setRequired(true);
		searchBtn = new PNativeButton(TimsPortalLabels.getLabel("btn.search"));
		searchBtn.setWidth("100px");
		searchBtn.addListener((ClickListener) this);		
		
		container = new BeanItemContainer<OrgStructure>(OrgStructure.class);
		table = new Table("STRUKTURAT ORGANIZATIVE", container);
		table.setVisibleColumns(COL_ORDER);
		table.setColumnHeaders(HEADERS);
		table.setSelectable(true);
		table.setSizeFull();
		table.setImmediate(true);
		table.addListener(new Property.ValueChangeListener() {			
			@Override
			public void valueChange(ValueChangeEvent event) {
				Object o = event.getProperty().getValue();
				if(o==null)return;
				OrgStructure os = (OrgStructure) o; 
				searchListener.searchFinished(os);
				OrgStructureWindow.this.getParent().removeWindow(OrgStructureWindow.this);
			}
		});
		
		HorizontalLayout hl = new HorizontalLayout();
		hl.setSpacing(true);
		hl.addComponentAsFirst(searchField);
		hl.addComponent(searchBtn);
				
		VerticalLayout vl = (VerticalLayout) getContent();
		vl.setSpacing(true);
		vl.addComponent(hl);
		vl.addComponent(table);
	}
	
	public void clear(){
		searchField.setValue("");
		container.removeAllItems();
	}
	
	public void addSearchListener(SearchListener<OrgStructure> listener){
		this.searchListener = listener;
	}

	@Override
	public void buttonClick(ClickEvent event) {
		if(!searchField.isValid())
			return;
		List<OrgStructure> list = portalBean.getRmisOrgStructureList(searchField.getValue().toString());
		container.removeAllItems();
		if(list!=null)
			container.addAll(list);
	}
	
	@Override
	public void attach(){
		super.attach();
		portalBean = this.getApplication().getPortalBean();
	}

}
